package com.ks.composestudy.ui

import androidx.annotation.DrawableRes
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.ks.composestudy.R

@Composable
fun BottomBar(selectedIndex: Int, onSelectChanged: (Int) -> Unit) {
    val colorSelected = Color(0xFFF9D865)
    val colorUnSelected = Color.Black
    Row(Modifier.background(Color.White)) {
        TabItem(
            if (selectedIndex == 0) R.drawable.ic_home else R.drawable.ic_home,
            "首页",
            if (selectedIndex == 0) colorSelected else colorUnSelected,
            Modifier.weight(1f).clickable {
                onSelectChanged(0)
            }, selectedIndex == 0
        )
        TabItem(
            if (selectedIndex == 1) R.drawable.ic_commend else R.drawable.ic_commend, "目录",
            if (selectedIndex == 1) colorSelected else colorUnSelected,
            Modifier
                .weight(1f)
                .clickable {
                    onSelectChanged(1)
                }, selectedIndex == 1
        )
        TabItem(
            if (selectedIndex == 2) R.drawable.ic_recovery else R.drawable.ic_recovery, "发现",
            if (selectedIndex == 2) colorSelected else colorUnSelected,
            Modifier.weight(1f).clickable {
                onSelectChanged(2)
            }, selectedIndex == 2
        )
        TabItem(
            if (selectedIndex == 3) R.drawable.ic_mine else R.drawable.ic_mine, "我",
            if (selectedIndex == 3) colorSelected else colorUnSelected,
            Modifier.weight(1f).clickable {
                onSelectChanged(3)
            }, selectedIndex == 3
        )
    }
}

@Composable
fun TabItem(
    @DrawableRes iconId: Int,
    title: String,
    color: Color,
    modifier: Modifier = Modifier,
    selected: Boolean
) {
    Column(
        modifier.padding(vertical = 10.dp),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        if (selected) {
            Image(
                painterResource(iconId),
                title,
                Modifier.size(24.dp).unread(true)
            )
        } else {
            Icon(painterResource(iconId), title, Modifier.size(24.dp), tint = color)
        }
        Text(
            text = title, fontSize = 16.sp, color = color
        )
    }
}

//@Preview(showBackground = true)
//@Composable
//fun TestPreview() {
//    var selectedTab by remember { mutableStateOf(0) }
//    BottomBar(selectedTab) {
//        selectedTab = it
//    }
//}